package com.example.demo.config;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;

/**
 * @author Admin
 */
@Data
@AllArgsConstructor //生成全参数构造函数
@NoArgsConstructor//生成无参构造函数
@Builder
public class Result<T> {
    /**
     * 调用是否成功标识，0：成功，-1:系统繁忙，此时请开发者稍候再试 详情见[ExceptionCode]
     */
    @ApiModelProperty(value = "响应编码:0/200-请求处理成功")
    private int code;

    @ApiModelProperty(value = "响应数据")
    private T data;

    @ApiModelProperty(value = "提示消息")
    private String msg = "成功";

    /**
     * 响应时间
     */
    @ApiModelProperty(value = "响应时间戳")
    private long timestamp = System.currentTimeMillis();

    /**
     * 系统报错时，抛出的原生信息
     */
    @ApiModelProperty(value = "异常消息")
    private String errorMsg = "";

    @ApiModelProperty(value = "请求路径")
    private String path;
    
    private static final int SUCCESS_CODE=200;
    public static final int FAIL_CODE = -1;

    public static final String DEF_ERROR_MESSAGE = "系统繁忙，请稍候再试";
    public static final String HYSTRIX_ERROR_MESSAGE = "请求超时，请稍候再试";


    public Result(int code,T data) {
        this.code = code;
        this.data = data;
    }

    public Result(int code,T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public Result(int code,T data, String msg, String errorMsg, String path) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.errorMsg = errorMsg;
        this.path = path;
    }

    /**
     * 成功
     */
    public static <T> Result<T> success() {
       return new Result<>(SUCCESS_CODE,null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE,data);
    }

    public static <T> Result<T> success(T data, String msg) {
        return new Result<>(SUCCESS_CODE,data,msg);
    }

    /**
     * 失败
     */
    public static <T> Result<T> error() {
        return new Result<>(FAIL_CODE,null,DEF_ERROR_MESSAGE);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code,null,msg);
    }

    public static <T> Result<T> error(int code, String msg, String errorMsg) {
        return new Result<>(code,null,msg,errorMsg,null);
    }

    public static <T> Result<T> error(int code, String msg, String errorMsg,String path) {
        return new Result<>(code,null, msg, errorMsg, path);
    }

    public static void main(String[] args) {
        System.out.println(Result.success(Result.success(Result.success())));
    }
}
